package com.ruoyi.web.cp.service.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.cp.domain.rm.CpAo5;
import com.ruoyi.web.cp.service.WinxinService;
import com.ruoyi.web.mapper.CpAo5Mapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author xiesm
 * @Description: TODO
 * @date 2025-08-21 16:56
 */
@Slf4j
@Service
public class Ao52Service {
    @Autowired
    private CpAo5Mapper cpAo5Mapper;
    @Autowired
    private WinxinService winxinService;
    private static final String[][] indexs = new String[][]{
            {"preDrawCode1", "preDrawCode2"},
            {"preDrawCode1", "preDrawCode3"},
            {"preDrawCode1", "preDrawCode4"},
            {"preDrawCode1", "preDrawCode5"},
            {"preDrawCode2", "preDrawCode3"},
            {"preDrawCode2", "preDrawCode4"},
            {"preDrawCode2", "preDrawCode5"},
            {"preDrawCode3", "preDrawCode4"},
            {"preDrawCode3", "preDrawCode5"},
            {"preDrawCode4", "preDrawCode5"},
    };

    public JSONObject getAo52_new(String index1, String index2) {
        List<CpAo5> cpAo5s = cpAo5Mapper.selectTop5(null);
        CpAo5 cpAo5 = cpAo5s.get(0);
        return getAo52List(index1, index2,
                cpAo5.getPreDrawCodeByIndex(index1),
                cpAo5.getPreDrawCodeByIndex(index2), 1, 2, null, cpAo5);
    }

    public JSONObject getAo52(String index1, String index2) {
        List<CpAo5> cpAo5s = cpAo5Mapper.selectTop5(null);
        CpAo5 cpAo5 = cpAo5s.get(1);
        return getAo52List(index1, index2,
                cpAo5.getPreDrawCodeByIndex(index1),
                cpAo5.getPreDrawCodeByIndex(index2), 0, 2, null, cpAo5s.get(0));
    }

    public void checkAo5_2(String maxIssue) {
        List<CpAo5> cpAo5s = cpAo5Mapper.selectTop5(maxIssue);
        CpAo5 cpAo5_1 = cpAo5s.get(1);
        for (int i = 0; i < indexs.length; i++) {
            String index1 = indexs[i][0];
            String index2 = indexs[i][1];
            JSONObject result = getAo52List(index1, index2, cpAo5_1.getPreDrawCodeByIndex(index1),
                    cpAo5_1.getPreDrawCodeByIndex(index2), 0, 2, maxIssue, cpAo5_1);
            CpAo5 cpAo5_0 = cpAo5s.get(0);
            String code1 = cpAo5_0.getPreDrawCodeByIndex(index1);
            String code2 = cpAo5_0.getPreDrawCodeByIndex(index2);
            if (result.getString("code0").equals(code1 + code2)
                    && result.getString("code1").equals(code1 + code2)) {
                result.fluentPut("code1", code1);
                result.fluentPut("code2", code2);
                String msg = StrUtil.format("{}\n" +
                                "本期:{}\n" +
                                "上期:{}\n" +
                                "{},{}\n" +
                                "{},{}",
                        cpAo5_0.getPreDrawIssue(),
                        cpAo5_0.getPreDrawCode(),
                        cpAo5s.get(1).getPreDrawCode(),
                        index1, index2,
                        code1, code2);
//                FileUtil.writeLines(Collections.singletonList(msg), "D:/ao5_2.txt", "utf8", true);
                winxinService.sendQlMsg("d89ed094-b15a-47df-8aa6-b9a90d990eed", msg);
            }
        }
    }

    public JSONObject getAo52List(String index1, String index2, String code1, String code2,
                                  int offset, int limit, String maxIssue, CpAo5 cpAo5_0) {
        JSONObject result = new JSONObject();
        List<String> issues = cpAo5Mapper.selectPreDrawCode2(index1, index2, code1, code2, offset,
                limit, maxIssue);
        List<String> list = new ArrayList<>();
        for (String issue : issues) {
            list.add(Long.valueOf(issue) + 1 + "");
        }
        List<String> allCodes = allCodes();

        List<CpAo5> cpAo5s = cpAo5Mapper.selectByPreDrawIssues(list);
        for (int i = 0; i < cpAo5s.size(); i++) {
            CpAo5 cpAo5 = cpAo5s.get(i);
            String preDrawCode1 = cpAo5.getPreDrawCodeByIndex(index1);
            String preDrawCode2 = cpAo5.getPreDrawCodeByIndex(index2);
            String code = preDrawCode1 + preDrawCode2;
            result.fluentPut("code" + i, code);

            allCodes.remove(code);
        }
        result.fluentPut("allCodes", CollUtil.join(allCodes, ","));
        result.fluentPut("thisCode", cpAo5_0.getPreDrawCode());
        return result;
    }

    private List<String> allCodes() {
        List<String> codes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                codes.add(i + "" + j);
            }
        }
        return codes;
    }

    public void checkHistory() {
        List<CpAo5> ao5s = cpAo5Mapper.selectAll();
        for (int i = 1; i < ao5s.size() - 1; i++) {
            CpAo5 ao5 = ao5s.get(i);
            for (int j = 0; j < ao5s.size(); j++) {
                CpAo5 subAo5 = ao5s.get(j);
                if (subAo5.getPreDrawIssueToLong() >= ao5.getPreDrawIssueToLong()) {
                    continue;
                }


            }
        }
    }
}
